//
//  WBHomeViewController.swift
//  徐工微博
//
//  Created by ahfu on 2017/4/8.
//  Copyright © 2017年 ahfu. All rights reserved.
//

import UIKit
//全局常亮 ，尽量用private修饰，
fileprivate let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {
    fileprivate lazy var statusList = [String]()
    //加载数据
    override func loadData(){
        //开始加载
        print("开始加载")
        //模拟延时加载数据
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            for i in 0..<15 {
                if self.isPullup {
                    //数据追加到底部
                    self.statusList.append("上啦\(i)")
                }else {
                    self.statusList.insert(i.description, at: 0)
                }
            }
            print("刷新表格")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //回复上啦刷新标记
            self.isPullup = false
            //刷新表格
            self.tableView?.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //setupUI()
        // Do any additional setup after loading the view.
    }
    
    @objc fileprivate func showFriends() {
        print(#function)
        
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}
//表格数据源方法
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1 取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        //2 设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //3 返回cell
        return cell
    }
    
}

extension WBHomeViewController {
    //重写父类的方法
    override func setupUI() {
        super.setupUI()
        
        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
